package com.whjfxx.terminal.utils;

import java.util.Random;
import java.util.UUID;

/**
 * Created by b.c on 2022/5/7 14:05
 */
public class CodeUtil {

    final static char[] STR1 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    final static char[] STR2 = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    final static char[] STR3 = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    final static char[] STR4 = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '@', '#', '$', '_'};

    final static Random random = new Random();

    /**
     * 随机生成 （包含数字）
     * @param len
     * @return
     */
    public static String genNum(int len) {
        int i, count = 0;
        StringBuffer number = new StringBuffer("");
        while (count++ < len) {
            i = Math.abs(random.nextInt(STR1.length));
            if (i >= 0 && i < STR1.length) {
                number.append(STR1[i]);
            }
        }
        return number.toString();
    }

    /**
     * 随机生成 （包含字母）
     * @param len
     * @return
     */
    public static String genCharacter(int len) {
        int i, count = 0;
        StringBuffer number = new StringBuffer("");
        while (count++ < len) {
            i = Math.abs(random.nextInt(STR2.length));
            if (i >= 0 && i < STR2.length) {
                number.append(STR2[i]);
            }
        }
        return number.toString();
    }

    /**
     * 随机生成 （包含数字和字母）
     * @param len
     * @return
     */
    public static String genCode(int len) {
        int i, count = 0;
        StringBuffer number = new StringBuffer("");
        while (count++ < len) {
            i = Math.abs(random.nextInt(STR3.length));
            if (i >= 0 && i < STR3.length) {
                number.append(STR3[i]);
            }
        }
        return number.toString();
    }

    /**
     * 随机生成 (包含 数字/字母/特殊符号)
     * @param len
     * @return
     */
    public static String genPwd(int len) {
        int i, count = 0;
        StringBuffer pwd = new StringBuffer("");
        while (count++ < len) {
            i = Math.abs(random.nextInt(STR4.length));
            if (i >= 0 && i < STR4.length) {
                pwd.append(STR4[i]);
            }
        }
        return pwd.toString();
    }

    /**
     * 随机生成uuid
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static void main(String[] args) {
        System.out.println(STR4.length);
        int i = 0;

        while(i++ < 1000) {
            String pwd = genCode(4);
            System.out.println(pwd);
        }
    }
}
